import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login/Login.vue'

// import Home from '@/components/Home/Home.vue'
// import Users from '@/components/Users/Users.vue'
// import Roles from '@/components/Roles/Roles.vue'
// import Rights from '@/components/Rights/Rights.vue'
// import Goods from '@/components/Goods/Goods.vue'
// import Categories from '@/components/Categories/Categories.vue'
// import GoodsAdd from '@/components/GoodsAdd/GoodsAdd.vue'
const Home = () => import('@/components/Home/Home.vue')
const Users = () => import('@/components/Users/Users.vue')
const Roles = () => import('@/components/Roles/Roles.vue')
const Rights = () => import('@/components/Rights/Rights.vue')
const Categories = () => import('@/components/Categories/Categories.vue')
// const Goods = () => import('@/components/Goods/Goods.vue')
// const GoodsAdd = () => import('@/components/GoodsAdd/GoodsAdd.vue')
const Goods = () => import(/* webpackChunkName:'goods' */ '@/components/Goods/Goods.vue')
const GoodsAdd = () => import(/* webpackChunkName:'goods' */ '@/components/GoodsAdd/GoodsAdd.vue')
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    children: [{
      path: '/users/:page?',
      component: Users
    },
    {
      path: '/roles',
      component: Roles
    },
    {
      path: '/rights',
      component: Rights
    },
    {
      path: '/goods',
      component: Goods
    },
    {
      path: '/categories',
      component: Categories
    },
    {
      path: '/goods-add',
      component: GoodsAdd
    }
    ]
  }
  ]
})
/**
 * 导航守卫
 * to 目标对象 to.path
 * from 来源对象
 * next 继续下一步 必须要执行
 */
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
